package com.xingxue.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet("/logout")
public class LogoutController extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("[调试信息] 已注销(清除session和cookie)");
		
		// session失效
		req.getSession().invalidate();
		
		// 删除cookie
		Cookie c = new Cookie("token", "");
		c.setHttpOnly(true);
		c.setMaxAge(0);
		c.setDomain("localhost");
		c.setPath("/demo_autologin");
		resp.addCookie(c);
		
		// 回到登录页面
		resp.sendRedirect("login.jsp");
	}

}
